package com.cinema.dao.imp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cinema.Bean.Admin;
import com.cinema.Bean.User;
import com.cinema.dao.BaseDao;
import com.cinema.dao.IAdminDao;
import com.mysql.jdbc.PreparedStatement;

/**
 * 用户表的mysql数据库实现类
 * **/

public class AdminDaoMysql extends BaseDao implements IAdminDao {
	public int insert(Admin a) {
		String sql = "insert into admin values(?,?,?)";
		String[] args = { a.getAdminId(), a.getAdminName(), a.getAdminPwd()};
		return change(sql, args);
	}

	public int delete(Admin a) {
		String sql = "delete from admin where adminId=?";
		String[] args = { a.getAdminId() };
		return change(sql, args);
	}

	public int update(Admin a) {
		String sql = "update admin set adminName=?,adminPwd=? where adminId=?";
		String[] args = { a.getAdminName(), a.getAdminPwd() ,a.getAdminId()};
		return change(sql, args);
	}
	
	/**
	 * 根据Id进行查询
	 */
	public Admin SelectById(String id) {
		// TODO Auto-generated method stub
		Admin a = null;
		String sql = "select * from admin where adminId=?";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql);
			pstmt.setString(1, id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				a = new Admin();
				a.setAdminId(rs.getString("adminId"));
				a.setAdminName(rs.getString("adminName"));
				a.setAdminPwd(rs.getString("adminPwd"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}

	/**
	 * 获取所有管理员信息
	 */
	public List<Admin> selectAll() {
		// TODO Auto-generated method stub
		List<Admin> list = new ArrayList<Admin>();
		Admin a = null;
		String sql = "select * from admin";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				a = new Admin();
				a.setAdminId(rs.getString("adminId"));
				a.setAdminName(rs.getString("adminName"));
				a.setAdminPwd(rs.getString("adminPwd"));
				list.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 根据关键词进行查询
	 */
	public List<Admin> Select(String keyword) {
		// TODO Auto-generated method stub
		List<Admin> list = new ArrayList<Admin>();
		Admin u = null;
		String sql = "select * from admin where adminId like '%" + keyword
		+ "%' or adminName like '%" + keyword + "%'";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				u = new Admin();
				u.setAdminId(rs.getString("adminId"));
				u.setAdminName(rs.getString("adminName"));
				u.setAdminPwd(rs.getString("adminPwd"));
				
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
